//
//  MCMineVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/4.
//

import UIKit
import SnapKit
import QMUIKit
import Toast_Swift
import ObjectMapper
import SwiftyJSON
import Kingfisher
import AutoInch

class MCMineVc: MCBaseVc {
    
    var mineGridView:QMUIGridView?
    
    
    var _headImageView:UIImageView!
    
    var _nameLabel:UILabel!
    
    var _vipLabel:UILabel!
    
    var _vipTipsLabel:MCPaddingLabel!
        
    var _openVipBtn:QMUIButton!
    
    var _userProfile:MCUserProfileModel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        //UI展示
        loadSubViews()
    }
    
    func loadSubViews() -> Void {
        
        let personInfoView = UIView.init()
        personInfoView.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        view.addSubview(personInfoView)
        personInfoView.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(16)
            make.right.equalTo(view.snp.right).offset(-16)
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(27)
            make.height.equalTo(72)
        }
        
        //添加点击事件
        let personTap = UITapGestureRecognizer(target: self, action: #selector(pushPersonInfoAction))
        personInfoView.addGestureRecognizer(personTap)
        

        //头像
        let headerImageView = UIImageView()
        headerImageView.qmui_borderColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        headerImageView.qmui_borderWidth = 1
        headerImageView.layer.cornerRadius = 36
        headerImageView.layer.masksToBounds = true
        personInfoView.addSubview(headerImageView)
        headerImageView.snp.makeConstraints { make in
            make.left.equalTo(personInfoView.snp.left).offset(0)
            make.top.equalTo(personInfoView.snp.top).offset(0)
            make.width.height.equalTo(72)
        }
        
        _headImageView = headerImageView
        
        
        //昵称
        let nickNameLabel = UILabel()
        nickNameLabel.text = "永乐yongle"
        nickNameLabel.font = UIFont(name: "PingFangSC-Semibold", size: 18)
        nickNameLabel.textColor = UIColor.qmui_color(withHexString: "#000000")
        personInfoView.addSubview(nickNameLabel)
        nickNameLabel.snp.makeConstraints { make in
            make.left.equalTo(headerImageView.snp.right).offset(16)
            make.top.equalTo(headerImageView.snp.top).offset(15)
            make.height.equalTo(25)
        }
        _nameLabel = nickNameLabel
        
        
        //会员展示
        let vipDisPlayLabel = MCPaddingLabel.init()
        vipDisPlayLabel.text = "Pro会员"
        vipDisPlayLabel.leftInset = 10
        vipDisPlayLabel.rightInset = 10
        vipDisPlayLabel.topInset = 5
        vipDisPlayLabel.bottomInset = 5
        vipDisPlayLabel.textColor = UIColor.qmui_color(withHexString: "#109B5E")
        vipDisPlayLabel.font = UIFont(name: "PingFangSC-Semibold", size: 12)
        vipDisPlayLabel.backgroundColor = UIColor.qmui_color(withHexString: "35DE94")?.withAlphaComponent(0.1)
        personInfoView.addSubview(vipDisPlayLabel)
        vipDisPlayLabel.snp.makeConstraints { make in
            make.centerY.equalTo(nickNameLabel.snp.centerY).offset(0.0)
            make.left.equalTo(nickNameLabel.snp.right).offset(5)
        
        }
        
        _vipTipsLabel = vipDisPlayLabel
        
        //副标题
        let editInfoLabel = UILabel()
        editInfoLabel.text = "编辑资料"
        editInfoLabel.textColor = UIColor.qmui_color(withHexString: "#777D83")
        editInfoLabel.font = UIFont(name: "PingFangSC-Regular", size: 12.auto())
        personInfoView.addSubview(editInfoLabel)
        editInfoLabel.snp.makeConstraints { make in
            make.left.equalTo(nickNameLabel.snp.left).offset(0)
            make.top.equalTo(nickNameLabel.snp.bottom).offset(5)
            make.height.equalTo(12)
        }
        
        //vip包裹图层
        let vipContainerView = UIView()
        vipContainerView.backgroundColor = UIColor.qmui_color(withHexString: "#10151C")
        vipContainerView.layer.cornerRadius = 10.0
        vipContainerView.layer.masksToBounds = true
        view.addSubview(vipContainerView)
        vipContainerView.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(16)
            make.right.equalTo(view.snp.right).offset(-16)
            make.top.equalTo(headerImageView.snp.bottom).offset(20)
            make.height.equalTo(63)
        }
        
        let vipContainerTap = UITapGestureRecognizer(target: self, action: #selector(vipPushProfile))
        vipContainerView.addGestureRecognizer(vipContainerTap)
        
        
        //文字-运动会员pro
        let actionLabel = QMUILabel.init()
        actionLabel.text = "运动会员Pro"
        actionLabel.textColor = UIColor.qmui_color(withHexString: "35DE94")
        actionLabel.font = UIFont(name: "PingFangSC-Semibold", size: 14)
        vipContainerView.addSubview(actionLabel)
        actionLabel.snp.makeConstraints { make in
            make.left.equalTo(vipContainerView.snp.left).offset(16)
            make.top.equalTo(vipContainerView.snp.top).offset(13)
            make.height.equalTo(20)
        }
        
        //会员日期--非会员显示
        let vipDateDisPlayLabel = QMUILabel.init()
        vipDateDisPlayLabel.text = "查看全部运作、Al聊天答疑、制定训练计划"
        vipDateDisPlayLabel.textColor = UIColor.white
        vipDateDisPlayLabel.font = UIFont(name: "PingFangSC-Regular", size: 10)
        vipContainerView.addSubview(vipDateDisPlayLabel)
        vipDateDisPlayLabel.snp.makeConstraints { make in
            make.left.equalTo(actionLabel.snp.left).offset(0.0)
            make.top.equalTo(actionLabel.snp.bottom).offset(5)
            make.height.equalTo(17)
        }
        
        _vipLabel = vipDateDisPlayLabel
        
        //开通VIP
        let openVipBtn = QMUIButton(type: .custom)
        openVipBtn.setTitle("开通", for: .normal)
        openVipBtn.setTitleColor(UIColor.qmui_color(withHexString: "#000000"), for: .normal)
        openVipBtn.titleLabel?.font = UIFont(name: "PingFangSC-Semibold", size: 14)
        openVipBtn.backgroundColor = UIColor.qmui_color(withHexString: "#35DE94")
        openVipBtn.layer.cornerRadius = 5
        openVipBtn.layer.masksToBounds = true
        vipContainerView.addSubview(openVipBtn)
        openVipBtn.snp.makeConstraints { make in
            make.right.equalTo(vipContainerView.snp.right).offset(-16)
            make.centerY.equalTo(vipContainerView.snp.centerY).offset(0)
            make.width.equalTo(48)
            make.height.equalTo(30)
        }
        
        _openVipBtn = openVipBtn
        
        
        let gridView = QMUIGridView(column: 4, rowHeight: 120)
        gridView?.separatorColor = UIColor.white
        gridView?.separatorWidth = 1
        gridView?.separatorDashed = false
        gridView?.backgroundColor = UIColor.white
        view.addSubview(gridView!)
        self.mineGridView = gridView
        
        
        // 将要布局的 item 以 addSubview: 的方式添加进去即可自动布局
        var indexCount:Int = 0
        
        let imageArr  = ["mine_my_colleciton_image","mine_body_data_image","mine_rechange_image","mine_fackback_image","mine_my_order_image","mine_soft_image","mine_setting_image","mine_share_master_image"]
        
        let titleArr = ["我的收藏","身体数据","兑换码","我要反馈","我的订单","软件评分","设置","分享Master"]
        
        for title in titleArr {
            
            let imagePosionButton =  QMUIButton.init();
            imagePosionButton.tintColorAdjustsTitleAndImage = UIColor.qmui_color(withHexString: "10151C")!
            imagePosionButton.spacingBetweenImageAndTitle = 8
            imagePosionButton.setImage(UIImage(named: imageArr[indexCount]), for: .normal)
            imagePosionButton.setTitle(title, for: .normal)
            imagePosionButton.imagePosition = QMUIButtonImagePosition.top
            imagePosionButton.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 13)
            imagePosionButton.addTarget(self, action: #selector(selectTapPush(action:)), for:.touchUpInside)
            indexCount += 1
            imagePosionButton.tag = 10 + indexCount
            gridView?.addSubview(imagePosionButton)
        }
        
        getLoadUserProfileData()
   
    }
    
    
    func getLoadUserProfileData() -> Void {
        
        self.view.makeToastActivity(.center)
        
        NetWorkRequest(MineVM.MineUseProfile) { ResponseModel in
            
        let userProfileModel = MCUserProfileModel.init(JSONString: ResponseModel.dataString)
            self._userProfile = userProfileModel
            self._headImageView.kf.setImage(with: URL(string: userProfileModel?.avatar ?? ""))
            self._nameLabel.text = userProfileModel?.name
            self.view.hideToastActivity()
            if userProfileModel?.memberExpireTime?.isEmpty == true {
                self._vipTipsLabel.isHidden = true
                self._openVipBtn.setTitle("开通", for: .normal)
                self._vipLabel.text = "查看全部运作、Al聊天答疑、制定训练计划"
            }else{
                self._vipTipsLabel.isHidden = false
                self._openVipBtn.setTitle("查看", for: .normal)
                self._vipLabel.text = userProfileModel?.memberExpireTime
            }
            
        } failureCallback: { ResponseModel in
            self.view.hideToastActivity()
            self.view.makeToast("网路失败",position: .center)
        }
    }
    
    
    //选择九宫格值
    @objc func selectTapPush(action:QMUIButton) -> Void {
        switch action.tag {
        case 11:
            let myCollectVc = MCMyCollectBaseVc.init()
            
            self.navigationController?.pushViewController(myCollectVc, animated: true)
           
        case 12:
            let bodyDataVc = MCBodyDataVc.init()
            bodyDataVc.userProfile = self._userProfile
            self.navigationController?.pushViewController(bodyDataVc, animated: true)
           
        
        case 13:
            
            let rechaneCodeVc = MCRechangeCodeVc.init()
            self.navigationController?.pushViewController(rechaneCodeVc, animated: true)
             
        
        case 14:
            
            let feedBackVc = MCFeedBackVc.init()
            self.navigationController?.pushViewController(feedBackVc, animated: true)
        
        case 15:
        
            let myOrderVc = MCMyOrderVc.init()
            
            self.navigationController?.pushViewController(myOrderVc, animated: true)
        
        case 16:
        
            print("16")
        
        case 17:
        
            let settingVc = MCSettingVc.init()
            settingVc.userProfile = self._userProfile
            self.navigationController?.pushViewController(settingVc, animated: true)
            
        
        case 18:
        
            print("18")
        
        default:
            print("无结果")
        }
    }
    
    @objc func vipPushProfile() -> Void {
        let vipPageVc = MCVipPageVc.init()
        vipPageVc.userProfile = self._userProfile
        self.navigationController?.pushViewController(vipPageVc, animated: true)
    }
    
    //
    @objc func pushPersonInfoAction() -> Void {
        let userProfileVc = MCUserProfileVc.init()
        userProfileVc.userProfile = self._userProfile
        self.navigationController?.pushViewController(userProfileVc, animated: true)
    }
    
    
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let padding = UIEdgeInsets.init(top: self.qmui_navigationBarMaxYInViewCoordinator, left: 16 + self.view.safeAreaInsets.left, bottom: 24 + self.view.safeAreaInsets.bottom, right: 16 + self.view.safeAreaInsets.right)
        let contentWidth = CGRectGetWidth(self.view.bounds) - UIEdgeInsetsGetHorizontalValue(padding)
        self.mineGridView?.frame = CGRect(x: padding.left, y: padding.top + 200, width:contentWidth, height: QMUIViewSelfSizingHeight)
        
    }
    
}


